package com.eden.context;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.eden.config.Configuration;

public class AppContext {
	private final static Log log = LogFactory.getLog(AppContext.class) ;
	private static ApplicationContext context ;
	private static Configuration configuration ;
	//保存当前运行中变量
	private static Map<String, Object> dataMap  ;
	
	public static ApplicationContext getContext() {
		return context;
	}

	public static boolean init(){
		try{
			dataMap = new HashMap<String, Object>() ;
			context = new ClassPathXmlApplicationContext("applicationContext*.xml") ;
			configuration = new Configuration();
			configuration.config() ;
			return true ;
		}catch(Exception e) {
			log.error("read the configuration file error" , e) ;
			return false ;
		}
	}

	public static Configuration getConfiguration() {
		return configuration;
	}

	public static void setConfiguration(Configuration configuration) {
		AppContext.configuration = configuration;
		configuration.persistent() ;
	}
	
	public static Object getAttribute(String key) {
		return dataMap.get(key) ;
	}
	
	public static void setAttribute(String key , Object value) {
		dataMap.put(key, value) ;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name){
		return (T) context.getBean(name) ;
	}
}
